/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.util.analyze;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import soccerscope.model.ClientScene;
import soccerscope.model.GameEvent;

public class PassChainAnalyzer extends PassAnalyzer {

	public static String NAME = "Pass Chain";

	int leftChain[] = new int[20];

	int rightChain[] = new int[20];

	@Override
	public void init() {
		super.init();
		this.leftChain = new int[20];
		this.rightChain = new int[20];
	}

	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public Object getValueAt(int col, int fromTime, int toTime) {
		count(fromTime, toTime);
		switch (col) {
		case ROW_NAME:
			return getName();
		case LEFT:
			if (this.lcount == -1)
				return "--";
			return new Integer(this.lcount);
		case RIGHT:
			if (this.rcount == -1)
				return "--";
			return new Integer(this.rcount);
		default:
			return " ";
		}
	}

	@Override
	public void count(int fromTime, int toTime) {
		this.lcount = 0;
		this.rcount = 0;
		for (int i = 0; i < 20; i++) {
			this.leftChain[i] = 0;
			this.rightChain[i] = 0;
		}
		for (int i = fromTime; i <= toTime; i++) {
			switch (PassAnalyzer.getPossessionTeam(i)) {
			case LEFT_SIDE:
				int left = 0;
				while (PassAnalyzer.getPossessionTeam(i) == LEFT_SIDE
						&& i <= toTime) {
					Pass pass = PassAnalyzer.getLeftPass(i);
					if (pass != null) {
						left++;
						i = pass.receiver.time;
						if (left > this.lcount)
							this.lcount = left;
					}
					i++;
				}
				this.leftChain[left]++;
				break;

			case RIGHT_SIDE:
				int right = 0;
				while (PassAnalyzer.getPossessionTeam(i) == RIGHT_SIDE
						&& i <= toTime) {
					Pass pass = PassAnalyzer.getRightPass(i);
					if (pass != null) {
						right++;
						i = pass.receiver.time;
						if (right > this.rcount)
							this.rcount = right;
					}
					i++;
				}
				this.rightChain[right]++;
				break;

			default:
			}
		}
		if (this.lcount == 0 || this.lcount == 1)
			this.lcount = -1;
		if (this.rcount == 0 || this.lcount == 1)
			this.rcount = -1;
	}

	@Override
	public GameEvent analyze(ClientScene clientScene, ClientScene prev) {
		return null;
	}

	public int getLeftPassChain(int chain) {
		return this.leftChain[chain];
	}

	public int getRightPassChain(int chain) {
		return this.rightChain[chain];
	}

	@Override
	public TableModel getTableModel() {
		TableModel dataModel = new AbstractTableModel() {
			@Override
			public String getColumnName(int col) {
				switch (col) {
				case 0:
					return " ";
				case 1:
					return "Left Team";
				case 2:
					return "Right Team";
				default:
					return " ";
				}
			}

			public int getColumnCount() {
				return 3;
			}

			public int getRowCount() {
				return 10;
			}

			public Object getValueAt(int row, int col) {
				count(0, PassChainAnalyzer.this.lastTime);
				switch (col) {
				case 0:
					return new Integer(row + 2);
				case 1:
					return new Integer(
							PassChainAnalyzer.this.leftChain[row + 2]);
				case 2:
					return new Integer(
							PassChainAnalyzer.this.rightChain[row + 2]);
				default:
					return " ";
				}
			}
		};

		return dataModel;
	}
}
